package com.api.shopapi.mapper;

import com.api.shopapi.entity.Institution;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface InstitutionMapper {

    @Insert("insert into institution" +
            " (" +
            "InstitutionCode," +
            "UserCode," +
            "CreateTime," +
            "Address," +
            "Lon," +
            "Lat," +
            "Type," +
            "districtRouter," +
            "location," +
            "GovernmentCode" +
            ") values (" +
            "#{institution.InstitutionCode}," +
            "#{institution.UserCode}," +
            "#{institution.CreateTime}," +
            "#{institution.Address}," +
            "#{institution.Lon}," +
            "#{institution.Lat}," +
            "#{institution.Type}," +
            "#{institution.districtRouter}," +
            "#{institution.location}," +
            "#{institution.GovernmentCode}" +
            ")")
    int insert(@Param("institution") Institution institution);

    @Update("update institution" +
            " set" +
            " Address=#{institution.Address}," +
            //" InsLevel=#{institution.InsLevel}," +
            " Lon=#{institution.Lon}," +
            " Lat=#{institution.Lat}," +
            " Type=#{institution.Type}," +
            " districtRouter=#{institution.districtRouter}," +
            " location=#{institution.location}" +
            " where InstitutionCode=#{institution.InstitutionCode}")
    int update(@Param("institution") Institution institution);

    @Delete("delete from institution where institutionCode=#{institutionCode}")
    int delete(@Param("institutionCode") String institutionCode);

    @Select("select * from institution where institutionCode=#{institutionCode} limit 1")
    Institution getByInstitutionCode(@Param("institutionCode") String institutionCode);


    @Select("select * from institution where userCode=#{userCode} limit 1")
    Institution getByUserCode(@Param("userCode") String userCode);

    @Select("<script>" +
            "select * from institution where GovernmentCode = #{governmentCode} " +
            " order by CreateTime desc " +
            "</script>")
    List<Institution> listByGovernmentCode(@Param("governmentCode") String governmentCode);


    @Select("<script>" +
            "select * from institution ins join user u" +
            " on ins.userCode = u.userCode" +
            " where ins.GovernmentCode = #{governmentCode} " +
            " <if test =' name !=\"\" '> " +
            " and u.Name like concat('%',#{name},'%')  " +
            " </if> " +
            " order by ins.CreateTime desc " +
            "</script>")
    List<Institution> listByGovCodeName(@Param("governmentCode") String governmentCode,
                                        @Param("name") String name);


    @Select("<script>" +
            "select * from institution" +
            " where GovernmentCode = #{governmentCode}" +
            " and type=#{type} " +
            " order by CreateTime desc " +
            "</script>")
    List<Institution> listByGovernmentCodeType(@Param("governmentCode") String governmentCode,
                                               @Param("type") int type);

    @Update("update institution" +
            " set" +
            " Address=#{institution.Address}," +
            //" InsLevel=#{institution.InsLevel}," +
            " Lon=#{institution.Lon}," +
            " Lat=#{institution.Lat}," +
            " Type=#{institution.Type}" +
            " where InstitutionCode=#{institution.InstitutionCode}")
    int updateAddressAndLatLngAndType(Institution institution);
}
